<?php
namespace app\api\model;
use think\Model;

class Ticket extends Model{
    protected $type = [
        'id' => 'integer',
        'type_id'    =>  'integer',
        'is_review'    =>  'integer',
        'income_review'    =>  'integer',
        'outlay_review'    =>  'integer',
        'cost_price'    =>  'float',
        'pay_price'    =>  'float',
        'sell_price'    =>   'float'
    ];
    public function type(){
        return $this->hasOne('TicketType','id','type_id')->bind(['type_name'=>'title']);
    }

    public function supplier(){
        return $this->hasOne('supplier','id','supplier_id')->field('id,supplier_name');
    }
    public function ticketList($filter = null){
        $where = [];
        $page = 1;
        $pageCount = 10;
        if(!empty($filter)){
            //指定id查询
            if(!empty($filter['id'])){
                $where['id'] = $filter['id'];
            }           
            //类型查询           
            if(!empty($filter['type_id'])){
                $where['type_id'] = $filter['type_id'];               
            }
            //页码            
            $page = !empty($filter['page']) ? $filter['page'] : $page;
            $pageCount = !empty($filter['page_count']) ? $filter['page_count'] : $pageCount;
        }
        $datas = $this->where($where)->with(['type','supplier'])->page("$page,$pageCount")->order('id', 'desc')->select();
        $dataCount =  $this->where($where)->count();
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => $dataCount
        );
        return $datalist;
    }
}